package com.hnevc.zz.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hnevc.zz.R;

import java.util.Timer;
import java.util.TimerTask;


public class LoginActivity extends AppCompatActivity {
    Button regist;
    CheckBox remmber_pwd,auto_login;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    String name,password;
    EditText editText1,editText2;
    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login1);
        regist=findViewById(R.id.btn_regist);
        editText1=(EditText)findViewById(R.id.et_userName);
        editText2=(EditText)findViewById(R.id.et_password);
        regist.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(LoginActivity.this,RegistActivity.class);
        startActivity(intent);
    }
});
        remmber_pwd=findViewById(R.id.cb_remember);
        auto_login=findViewById(R.id.cb_autologin);

        sharedPreferences=this.getSharedPreferences("UserInfo", Context.MODE_APPEND);
        //自动登陆
        if(sharedPreferences.getBoolean("autologin",false)){
            Toast.makeText(this, "已选中自动登录，将在3秒后跳转！", Toast.LENGTH_SHORT).show();
            auto_login.setChecked(true);
            remmber_pwd.setChecked(true);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            },3000);

        }
        //记住密码
        if(sharedPreferences.getBoolean("remmberpwd",false)){
            remmber_pwd.setChecked(true);
            editText1.setText(sharedPreferences.getString("UserName",""));
            editText2.setText(sharedPreferences.getString("UserPwd",""));
        }

        //登录
        Button button=(Button)findViewById(R.id.btn_login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText1=(EditText)findViewById(R.id.et_userName);
                 editText2=(EditText)findViewById(R.id.et_password);
                 name=editText1.getText().toString();
                 password=editText2.getText().toString();
                if(name.equals("admin")&&password.equals("123456")){
                    Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    if (remmber_pwd.isChecked()){
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString("UserName",name);
                        editor.putString("UserPwd",password);
                        editor.commit();
                    }
                    if (auto_login.isChecked()){
                        auto_login.setChecked(true);
                        remmber_pwd.setChecked(true);
                        SharedPreferences.Editor editor=sharedPreferences.edit();
                        editor.putString("UserName",name);
                        editor.putString("UserPwd",password);
                        editor.commit();
                       Toast.makeText(LoginActivity.this,"已选择自动登陆",Toast.LENGTH_SHORT).show();
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                        },3000);
                    }

                    Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    LayoutInflater layoutInflater=LayoutInflater.from(LoginActivity.this);
                    View textEntryView = layoutInflater.inflate(R.layout.nav_header, null);
                    ImageView imageView=(ImageView) textEntryView.findViewById(R.id.icon_image1);
                    imageView.setImageResource(R.drawable.oo);
                    finish();
                }
                else {
                    Toast.makeText(LoginActivity.this,"登录失败，请重新输入",Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

}
